﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FeedAntGame.Control;

namespace FeedAntGame.Sprite.Event
{
    sealed class CandyFeedSprite : EventSprite
    {
        Texture2D waitingForeground;
        int waitingRectOffset = 0;

       
        public enum STATUS { COLD_DOWN, READY, ACTIVE}
        private STATUS status = STATUS.READY;
        private int curColdown = 4000;
        private int coldown = 4000;
        private Candy.TYPE type;

        /************************************************************************/
        /*                      Get/Set                                                                     */
        /************************************************************************/
        
        /// <summary>
        /// Get type of candyFeed
        /// </summary>
        internal Candy.TYPE Type
        {
            get { return type; }
        }

        /// <summary>
        /// Get status of candyFeed
        /// </summary>
        internal STATUS Status
        {
            get { return status; }
        }


        public int WaitingRectOffset
        {
            get { return waitingRectOffset; }
            set { waitingRectOffset = value; }
        } 

        /// <summary>
        /// scale: miliSeconds
        /// </summary>
        public int Coldown
        {
            get { return curColdown; }
            set { curColdown = value; coldown = value; }
        }
        /************************************************************************/
        /*                      Constructor                                                                     */
        /************************************************************************/
        /// <summary>
        /// Default 4000 miliSeconds curColdown. Should call success action in event Click
        /// </summary>
        /// <param name="waitingRect"></param>
        /// <param name="clickBackground"></param>
        /// <param name="normalBackground"></param>
        /// <param name="hoverBackground"></param>
        /// <param name="disableBackground"></param>
        /// <param name="position"></param>
        /// <param name="frameSize"></param>
        /// <param name="collisionOffset"></param>
        /// <param name="sheetSize"></param>
        /// <param name="speed"></param>
        /// <param name="collisionCueName"></param>
        /// <param name="scale"></param>
        /// <param name="size"></param>
        /// <param name="curColdown"></param>
        public CandyFeedSprite(Texture2D waitingRect,Texture2D clickBackground, Texture2D normalBackground, Texture2D hoverBackground,
            Texture2D disableBackground, Vector2 position, Point frameSize,
            int collisionOffset, Point sheetSize, Vector2 speed, string collisionCueName, float scale,  int coldown,
            Candy.TYPE type)
            : base(clickBackground,normalBackground,hoverBackground,disableBackground,position,frameSize,
            collisionOffset,sheetSize,speed,collisionCueName,scale)        
        {
            this.waitingForeground = waitingRect;
            this.curColdown = coldown;
            this.coldown = coldown;
            this.type = type;
            
        }

        
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            base.Update(gameTime, clientBounds);
            //After base call, State properties update value.
            switch (Status)
            {
                case STATUS.COLD_DOWN:
                    curColdown -= gameTime.ElapsedGameTime.Milliseconds;
                    Enable = false;
                    if (curColdown <= 0)
                    {
                        Enable = true;
                        curColdown = coldown;
                        status = STATUS.READY;
                    }
                    break;
                
            }

           
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            base.Draw(gameTime, spriteBatch);
            // Draw waiting foreground after to show them out
            if(this.status == STATUS.COLD_DOWN)
            spriteBatch.Draw(waitingForeground,
                new Rectangle((int)position.X - waitingRectOffset, 
                    (int)(position.Y - waitingRectOffset + 1.0 *(coldown - curColdown) * (Height+waitingRectOffset*3) / coldown),
                    (int) (Width + waitingRectOffset*2),
                    (int)(Height + waitingRectOffset * 2 - 1.0 * (coldown - curColdown) * (Height + waitingRectOffset * 3) / coldown)), 
                Color.White);
        }
        /// <summary>
        /// Status properties change after call this.
        /// </summary>
        /// <param name="success"></param>
        public void SuccessAction(bool success)
        {
            if (Status == STATUS.ACTIVE)
            {
                if (success)
                {
                    status = STATUS.COLD_DOWN;
                }
                else
                {
                    status = STATUS.READY;
                }
            }
        }

        protected override void onClick()
        {
            base.onClick();
            if (status == STATUS.READY)
            {
                status = STATUS.ACTIVE;
            }
        }

        public void Reset()
        {
            this.status = STATUS.READY;
        }

    }
}
